﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Core.AdminMenu;
using Core.Authenticated;
using Core.Library;

namespace CMS.Controls.AdminMenu
{
    public partial class CreateOrEdit : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetListTreeMenu();

                ltTitle.Text = "Tạo mới chức năng";

                if (Request.QueryString[Utils.MenuId] != null)
                {
                    var menuId = Utils.GetIdFromRequestString(Utils.MenuId, 0);

                    if (menuId > 0)
                    {
                        ltTitle.Text = "Sửa thông tin chức năng";

                        BindMenu(menuId);
                    }
                }
            }
        }

        private void GetListTreeMenu()
        {
            var menus = new List<AdminMenuObject>();
            AdminMenuController.AdminMenu_GetTree(menus, 0, 0);

            if (menus.Count > 0)
            {
                ddlMenu.DataSource = menus;
                ddlMenu.DataBind();
            }
            ddlMenu.Items.Insert(0, new ListItem("-- Gốc --", "0"));
        }

        private void BindMenu(int id)
        {
            var menu = AdminMenuController.AdminMenu_GetById(id);
            if (menu != null)
            {
                txtName.Text = menu.Name;
                txtParams.Text = menu.Params;
                txtCtrlKey.Text = menu.CtrlKey;
                txtCtrlSource.Text = menu.CtrlSource;
                txtPriority.Text = menu.Priority.ToString();
                ddlMenu.SelectedValue = menu.ParentId.ToString();
                ddlStatus.SelectedValue = menu.Status.ToString();
            }
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            AdminMenuObject menu;

            var id = Utils.GetIdFromRequestString(Utils.MenuId, 0);

            var parentId = ConvertUtility.ConvertTo<int>(ddlMenu.SelectedValue);

            if (id > 0)
            {
                menu = AdminMenuController.AdminMenu_GetById(id);
                menu.AdminMenuId = id;
                menu.Name = txtName.Text;
                menu.ParentId = parentId;
                menu.CtrlKey = txtCtrlKey.Text;
                menu.CtrlSource = txtCtrlSource.Text;
                menu.Params = txtParams.Text;
                menu.Priority = ConvertUtility.ConvertTo<int>(txtPriority.Text);
                menu.Status = ConvertUtility.ConvertTo<int>(ddlStatus.SelectedValue);

                AdminMenuController.AdminMenu_Update(menu);
            }
            else
            {
                menu = new AdminMenuObject();
                menu.Name = txtName.Text;
                menu.ParentId = parentId;
                menu.CtrlKey = txtCtrlKey.Text;
                menu.CtrlSource = txtCtrlSource.Text;
                menu.Params = txtParams.Text;
                menu.Priority = ConvertUtility.ConvertTo<int>(txtPriority.Text);
                menu.Status = ConvertUtility.ConvertTo<int>(ddlStatus.SelectedValue);

                AdminMenuController.AdminMenu_Create(menu);
            }

            Response.Redirect(UrlHelpers.ListMenu);
        }        
    }
}